package me.like.diandian.fragment;

import android.os.Bundle;

import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.LinearLayoutManager;


import me.like.diandian.R;
import me.like.diandian.adapter.DaiKuanListAdapter;
import me.like.diandian.api.Api;
import me.like.diandian.base.BaseConstant;
import me.like.diandian.base.BaseFragment;
import me.like.diandian.databinding.RefreshRecyclerBinding;
import me.like.diandian.util.RxUtils;

/**
 * Created by shousakai on 2019/6/21.
 */

public class DaiKuanListFragment extends BaseFragment<RefreshRecyclerBinding> {
    private int type;
    private DaiKuanListAdapter adapter;

    @Override
    protected void initView() {
        type = getArguments().getInt("type", 1);
        binding.llParent.setBackgroundColor(ActivityCompat.getColor(getContext(), R.color.common_bg));
        binding.refresh.setOnRefreshListener((refreshlayout) -> {
            refresh();
        });
        binding.refresh.setEnableLoadmore(false);

        binding.idRecyclerview.setLayoutManager(new LinearLayoutManager(activity));
        refresh();

    }

    private void refresh() {
        Api.getInstance().getService().getLoanMarketList("0", BaseConstant.APPCHANNELID, type + "", "0", "0")
                .compose(RxUtils.applySchedulerAndAllFilter(activity))
                .subscribe(data -> {
                    adapter = new DaiKuanListAdapter(activity,data.getData());
                    binding.idRecyclerview.setAdapter(adapter);
                    binding.refresh.finishRefresh();
                }, Throwable::printStackTrace);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.refresh_recycler;
    }

    public static DaiKuanListFragment getInstance(int type) {
        DaiKuanListFragment fragment = new DaiKuanListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }
}
